package com.eascs.web.p2p.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.web.context.RequestContext;
import com.eascs.web.p2p.common.security.Configuration;
import com.eascs.web.p2p.member.controller.LoginRestController;

public class AccessInterceptorAdapter extends HandlerInterceptorAdapter {
	private final Logger logger = LoggerFactory.getLogger(AccessInterceptorAdapter.class);

	private final static String ACCESS_MEMBER_URL = "/member";
	private final static String ACCESS_UC_URL = "/uc";
	
	private final static String UN_SEND_BIND_PAY_REQUEST_CALLBACK= "/member/rest/unSendBindPayRequestCallBack";
	private final static String WITHDRAW_CALLBACK= "/member/rest/withdrawCallBack";
	private final static String WITHDRAW_ADMIN= "/member/rest/withdrawByAdmin";
	
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean isCheckAccess = false;
		String requestURL = RequestContext.getRequestURI();
		if (requestURL.indexOf(ACCESS_MEMBER_URL) != -1 || requestURL.indexOf(ACCESS_UC_URL) != -1) {
			isCheckAccess = true;
		}
		if (requestURL.indexOf(WITHDRAW_ADMIN) != -1) {
			isCheckAccess = false;
		}
		//支付接口回调无需用户验证
		if (requestURL.indexOf(UN_SEND_BIND_PAY_REQUEST_CALLBACK) != -1 || requestURL.indexOf(WITHDRAW_CALLBACK) != -1) {
			isCheckAccess = false;
		}
		if (isCheckAccess) {
			ModelResult<InvestorEntity> modelResult = LoginRestController.isLogin(request);
			if (!modelResult.isSuccess() || modelResult.getModel() == null) {
				throw new NoAccessException("No Access request :" + RequestContext.getRequestURI());
			}
		}
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
}
